﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using OrderManager.Orders;

namespace OrderManager.ViewModels
{
    class DownloadViewModel : DependencyObject
    {
        public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(DownloadViewModel));
        public static readonly DependencyProperty ProgressProperty = DependencyProperty.Register("Progress", typeof(double), typeof(DownloadViewModel));

        public string Message { get { return (string)this.GetValue(MessageProperty); } set { this.SetValue(MessageProperty, value); } }

        public double Progress { get { return (double)this.GetValue(ProgressProperty); } set { this.SetValue(ProgressProperty, value); } }

        public OrderManagerBase OrderManager { get; set; }

        public bool IsDownloadOK { get; private set; }

        public List<Order> Orders { get; private set; }

        public DownloadViewModel(OrderManagerBase orderManager)
        {
            this.Message = "";
            this.Progress = 0;
            this.Orders = new List<Order>();
            this.Attach(orderManager);
        }

        public void Attach(OrderManagerBase orderManager)
        {
            this.OrderManager = orderManager;
            this.OrderManager.OrderDownloadStart += new EventHandler(orderManager_OrderDownloadStart);
            this.OrderManager.OrderDownloading += new EventHandler<OrderDownloadEventArgs>(orderManager_OrderDownloading);
            this.OrderManager.OrderDownloadEnd += new EventHandler<DownloadEndEventArgs>(OrderManager_OrderDownloadEnd);
        }

        void orderManager_OrderDownloadStart(object sender, EventArgs e)
        {
            this.Orders.Clear();
            this.IsDownloadOK = false;
            this.Dispatcher.Invoke(new Action(() =>
            this.Message = "已开始下载"));
        }

        void orderManager_OrderDownloading(object sender, OrderDownloadEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                this.Progress = e.CurrentCount * 1.0 / e.Total;
                this.Message = string.Format("正在下载{0}/{1}...", e.CurrentCount, e.Total);
                this.Orders.Add(e.Order);
            }));
        }

        void OrderManager_OrderDownloadEnd(object sender, DownloadEndEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
                {
                    if (e.Exception == null)
                    {
                        this.Progress = 1;
                        this.Message = "下载完成";
                    }
                    else
                    {
                        this.Message = e.Exception.Message;
                    }
                }));
        }

        public void Deattach()
        {
            this.OrderManager.OrderDownloadStart -= new EventHandler(orderManager_OrderDownloadStart);
            this.OrderManager.OrderDownloading -= new EventHandler<OrderDownloadEventArgs>(orderManager_OrderDownloading);
            this.OrderManager.OrderDownloadEnd -= new EventHandler<DownloadEndEventArgs>(OrderManager_OrderDownloadEnd);
            this.OrderManager = null;
        }
    }
}
